Async এবং Await হল C# এর asynchronous programming এর গুরুত্বপূর্ণ অংশ, যা ইউজার ইন্টারফেসের জন্য অপ্রয়োজনীয় ব্লকিং এড়াতে সাহায্য করে। MVVM প্যাটার্নে, এই কনসেপ্ট ব্যবহার করে আপনি UI থ্রেডকে অব্যাহত রাখতে পারেন যখন ViewModel এর মধ্যে ডেটা লোড বা অন্যান্য সময়সাপেক্ষ অপারেশন চলমান থাকে। এর মাধ্যমে অ্যাপ্লিকেশন দ্রুত এবং রেসপন্সিভ হয়।
MVVM প্যাটার্নে, ViewModel সাধারণত Model থেকে ডেটা নিয়ে আসে এবং View এর সাথে ডেটা বাইন্ডিং করে। যখন ডেটা লোড করা, API কল, বা ডেটাবেস অপারেশন করা হয়, তখন এটি UI থ্রেডকে ব্লক না করার জন্য async এবং await ব্যবহার করা উচিত।
ধরা যাক, আমরা একটি WeatherApp তৈরি করছি যেখানে Weather ডেটা API থেকে আসবে। ViewModel এ async এবং await ব্যবহার করে এই ডেটা ফেচ করা হবে।
public class Weather
{
public string Location { get; set; }
public string Temperature { get; set; }
}
এখন, একটি API কল করি যা Weather ডেটা নিয়ে আসবে।
public class WeatherService
{
private HttpClient _httpClient;
public WeatherService()
{
_httpClient = new HttpClient();
}
public async Task<Weather> GetWeatherAsync(string location)
{
string url = $"https://api.weatherapi.com/v1/current.json?key=YOUR_API_KEY&q={location}";
HttpResponseMessage response = await _httpClient.GetAsync(url);
if (response.IsSuccessStatusCode)
{
var weatherData = await response.Content.ReadAsAsync<Weather>();
return weatherData;
}
return null;
}
}
ViewModel এ async এবং await ব্যবহার করে ডেটা লোড করা হবে। INotifyPropertyChanged ইন্টারফেসের মাধ্যমে ডেটা পরিবর্তনকে UI তে প্রতিফলিত করা হবে।
public class WeatherViewModel : INotifyPropertyChanged
{
private string _location;
private string _temperature;
private WeatherService _weatherService;
public string Location
{
get { return _location; }
set
{
_location = value;
OnPropertyChanged(nameof(Location));
}
}
public string Temperature
{
get { return _temperature; }
set
{
_temperature = value;
OnPropertyChanged(nameof(Temperature));
}
}
public WeatherViewModel()
{
_weatherService = new WeatherService();
}
public async Task LoadWeatherDataAsync()
{
// Location পরিবর্তনের সাথে ডেটা লোড করা
var weather = await _weatherService.GetWeatherAsync(Location);
if (weather != null)
{
Temperature = weather.Temperature;
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
এখন, View তে ডেটা বাইন্ডিং সেট করে দেব। LoadWeatherDataAsync মেথডটি Button ক্লিকের মাধ্যমে কল হবে।
<Window x:Class="WeatherApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WeatherApp" Height="350" Width="525">
<Window.DataContext>
<local:WeatherViewModel />
</Window.DataContext>
<Grid>
<TextBox Text="{Binding Location}" HorizontalAlignment="Left" VerticalAlignment="Top" Width="200"/>
<Button Content="Get Weather" HorizontalAlignment="Right" VerticalAlignment="Top" Width="100"
Command="{Binding GetWeatherCommand}"/>
<TextBlock Text="{Binding Temperature}" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,50,0,0"/>
</Grid>
</Window>
এখানে, TextBox তে ব্যবহারকারী Location ইনপুট দিবে, এবং Button ক্লিক হলে LoadWeatherDataAsync মেথডটি চলবে। Temperature প্রপার্টি TextBlock-এ বাইন্ড করা হয়েছে।
Async এবং Await ব্যবহার করে MVVM প্যাটার্নে UI থ্রেডের উপর চাপ কমানো যায় এবং ডেটা লোড বা অন্যান্য সময়সাপেক্ষ অপারেশন করতে হয় এমন ক্ষেত্রে অ্যাপ্লিকেশনকে স্নিগ্ধ এবং রেসপন্সিভ রাখা সম্ভব হয়।
common.read_more